<?php
namespace app\models;
use common\component\tool\Tool;
use app\models\Basic;

use Yii;
use yii\helpers\Html;

class Content extends Basic
{
    
    public static function tableName()
    {
        return '{{%content}}';
    }

    public function rules()
    {
        return [
            ['app_id', 'required', 'message' => 'app_id不能为空', 'on' => 'add'],
            ['app_id', 'integer', 'message' => 'app_id必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'app_id最小存储0', 
                        'max'=>18446744073709551615, 'tooBig' => 'app_id最大存储18446744073709551615'],
            ['catids', 'required', 'message' => 'catids不能为空', 'on' => 'add'],
            ['catids', 'string', 'message' => 'catids必须是字符串',
                                // 'min' => 2, 'tooShort' => 'catids最短2位',
                                'max' => 512, 'tooLong' => 'catids最长512位'],
            ['catid', 'required', 'message' => 'catid不能为空', 'on' => 'add'],
            ['catid', 'integer', 'message' => 'catid必须为数字，或数字字符串', 
                     'min'=>0, 'tooSmall' => 'catid最小存储0', 
                     'max'=>65535, 'tooBig' => 'catid最大存储65535'],
            ['lang', 'required', 'message' => 'lang不能为空', 'on' => 'add'],
            ['lang', 'string', 'message' => 'lang必须是字符串',
                                'min' => 1, 'tooShort' => 'lang最短1位',
                                'max' => 8, 'tooLong' => 'lang最长8位'],
            ['title', 'required', 'message' => 'title不能为空', 'on' => 'add'],
            ['title', 'string', 'message' => 'title必须是字符串',
                                'min' => 1, 'tooShort' => 'title最短1位',
                                'max' => 64, 'tooLong' => 'title最长64位'],
            ['titlecolor', 'string', 'message' => 'titlecolor必须是字符串',
                                'max' => 8, 'tooLong' => 'titlecolor最长8位'],
            ['flag', 'default', 'value' => '', 'on' => 'add'],
            ['flag', 'string', 'message' => 'flag必须是字符串',
                                'min' => 1, 'tooShort' => 'flag最短1位',
                                'max' => 64, 'tooLong' => 'flag最长64位'],
            ['preview', 'default', 'value' => '', 'on' => 'add'],
            ['preview', 'string', 'message' => 'preview必须是字符串',
                                'min' => 2, 'tooShort' => 'preview最短2位',
                                'max' => 512, 'tooLong' => 'preview最长512位'],

            ['source', 'string', 'message' => 'source必须是字符串',
                                'max' => 128, 'tooLong' => 'source最长128位'],
            ['author', 'string', 'message' => 'author必须是字符串',
                                'max' => 18, 'tooLong' => 'author最长18位'],
            ['outside', 'default', 'value' => 1, 'on' => 'add'],
            ['outside', 'integer', 'message' => 'outside必须为数字，或数字字符串'],
            ['outside', 'in', 'range' => array(1, 2), 'message' => 'outside值必须在1,2中选择'],
            ['url_target', 'default', 'value' => 2, 'on' => 'add'],
            ['url_target', 'integer', 'message' => 'url_target必须为数字，或数字字符串'],
            ['url_target', 'in', 'range' => array(1, 2), 'message' => 'url_target值必须在1,2中选择'],
            ['outside_url', 'default', 'value' => '', 'on' => 'add'],
            ['outside_url', 'string', 'message' => 'outside_url必须是字符串',
                                'min' => 2, 'tooShort' => 'outside_url最短2位',
                                'max' => 512, 'tooLong' => 'outside_url最长512位'],

            ['keyword', 'default', 'value' => '', 'on' => 'add'],
            ['keyword', 'string', 'message' => 'keyword必须是字符串',
                                'min' => 2, 'tooShort' => 'keyword最短2位',
                                'max' => 512, 'tooLong' => 'keyword最长128位'],
            ['describe', 'default', 'value' => '', 'on' => 'add'],
            ['describe', 'string', 'message' => 'describe必须是字符串',
                                'min' => 2, 'tooShort' => 'describe最短2位',
                                'max' => 512, 'tooLong' => 'describe最长512位'],
            ['content', 'default', 'value' => '', 'on' => 'add'],
            ['content', 'string', 'message' => 'content必须是字符串'],

            ['state', 'default', 'value' => 1, 'on' => 'add'],
            ['state', 'integer', 'message' => 'state必须为数字，或数字字符串'],
            ['state', 'in', 'range' => array(1, 2, 3, 4, 5), 'message' => 'state值必须在1~5中选择'],
            ['rejected_reason', 'default', 'value' => '', 'on' => 'add'],
            ['rejected_reason', 'string', 'message' => 'rejected_reason必须是字符串',
                                'min' => 2, 'tooShort' => 'rejected_reason最短2位',
                                'max' => 512, 'tooLong' => 'rejected_reason最长512位'],
            ['audit_time', 'default', 'value' => '1000-01-01 00:00:00', 'on' => 'add'],
            ['audit_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'audit_time非yyyy-MM-dd HH:mm:ss格式'],
            ['member_view', 'default', 'value' => 1, 'on' => 'add'],
            ['member_view', 'integer', 'message' => 'member_view必须为数字，或数字字符串'],
            ['member_view', 'in', 'range' => array(1, 2), 'message' => 'member_view值必须在1,2中选择'],
            ['previewtime', 'integer', 'message' => 'previewtime必须为数字，或数字字符串', 
                     'min'=>0, 'tooSmall' => 'previewtime最小存储0', 
                     'max'=>255, 'tooBig' => 'previewtime最大存储255'],
            ['integral', 'integer', 'message' => 'integral必须为数字，或数字字符串', 
                     'min'=>0, 'tooSmall' => 'integral最小存储0', 
                     'max'=>65535, 'tooBig' => 'integral最大存储65535'],
            
            ['display', 'integer', 'message' => 'display必须为数字，或数字字符串'],
            ['display', 'in', 'range' => array(1, 2), 'message' => 'display值必须在1,2中选择'],
            ['istop', 'default', 'value' => 1, 'on' => 'add'],
            ['istop', 'integer', 'message' => 'istop必须为数字，或数字字符串'],
            ['istop', 'in', 'range' => array(1, 2), 'message' => 'istop值必须在1,2中选择'],
            ['toptime', 'default', 'value' => 0, 'on' => 'add'],
            ['toptime', 'integer', 'message' => 'toptime必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'toptime最小存储0', 
                        'max'=>18446744073709551615, 'tooBig' => 'toptime最大存储18446744073709551615'],

            ['pc_preview_count', 'default', 'value' => 0, 'on' => 'add'],
            ['pc_preview_count', 'integer', 'message' => 'pc_preview_count必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'pc_preview_count最小存储0', 
                        'max'=>18446744073709551615, 'tooBig' => 'pc_preview_count最大存储18446744073709551615'],
            ['mobile_preview_count', 'default', 'value' => 0, 'on' => 'add'],
            ['mobile_preview_count', 'integer', 'message' => 'mobile_preview_count必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'mobile_preview_count最小存储0', 
                        'max'=>18446744073709551615, 'tooBig' => 'mobile_preview_count最大存储18446744073709551615'],
            ['sort', 'integer', 'message' => 'sort必须为数字，或数字字符串', 
                     'min'=>0, 'tooSmall' => 'sort最小存储0', 
                     'max'=>18446744073709551615, 'tooBig' => 'sort最大存储18446744073709551615'],
            ['lastmodify_time', 'default', 'value' => '1000-01-01 00:00:00', 'on' => 'add'],
            ['lastmodify_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'lastmodify_time非yyyy-MM-dd HH:mm:ss格式'],
            ['add_time', 'default', 'value' => '1000-01-01 00:00:00', 'on' => 'add'],
            ['add_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'add_time非yyyy-MM-dd HH:mm:ss格式'],
        ];
    }


    /*添加*/
    public function addContent($data, $app_id)
    {
        // P($data);changeCatidFormat
        $this->scenario = 'add';
        $data['Content']['app_id'] = $app_id;
        if(isset($data['Content']['catids']) and is_array($data['Content']['catids'])){
            $data['Content']['catid'] = $data['Content']['catids'][0]['catid'];
            $data['Content']['catids'] = $this->changeCatidFormat($data['Content']['catids']);
        }
        if(isset($data['Content']['flag']) and is_array($data['Content']['flag'])){
            $data['Content']['flag'] = implode(',', $data['Content']['flag']);
        }
        if(isset($data['Content']['preview']) and is_array($data['Content']['preview'])){
            $data['Content']['preview'] = Tool::changeUploadFormat($data['Content']['preview']);
        }
        if(isset($data['Content']['outside'])){
            $data['Content']['outside'] = Tool::changeBool($data['Content']['outside']);
        }
        if(isset($data['Content']['member_view'])){
            $data['Content']['member_view'] = Tool::changeBool($data['Content']['member_view']);
        }
        if(isset($data['Content']['state']) and $data['Content']['state'] == 3){//说明是需要审核
            $data['Content']['audit_time'] = date('Y-m-d H:i:s', time());
        }
        if(isset($data['Content']['display'])){
            $data['Content']['display'] = Tool::changeBool($data['Content']['display']);
        }
        if(isset($data['Content']['istop'])){
            $data['Content']['istop'] = Tool::changeBool($data['Content']['istop']);
            if($data['Content']['istop'] == 2){
                $data['Content']['toptime'] = time();
            }else{
                $data['Content']['toptime'] = 0;
            }
        }else{
            $data['Content']['toptime'] = 0;
        }
        if(isset($data['Content']['content'])){
            $data['Content']['content'] = Html::encode($data['Content']['content']);
        }

        if(!isset($data['Content']['add_time']) or empty($data['Content']['add_time'])){
            $data['Content']['add_time'] = date('Y-m-d H:i:s', time());
        }
        // P($data);
        if($this->load($data) and $this->validate()){
            // P($this);
            if($this->save(false)){
                $id = $this->getPrimaryKey();
                $this->updateAll(['sort' => $id], 'content_id = :id', [':id' => $id]);
                return $id;
            }
        }
        return false;
    }

    /*
    修改
    $data       修改的数据
    $app_id 
    */
    public function modifyContent($data, $id)
    {
        // P($data);
        $content = self::find()->where('content_id = :id', [':id' => $id])->one();
        // P($content);
        if(isset($data['Content']['catids']) and is_array($data['Content']['catids'])){
            $data['Content']['catid'] = $data['Content']['catids'][0]['catid'];
            $data['Content']['catids'] = $this->changeCatidFormat($data['Content']['catids']);
        }
        if(isset($data['Content']['flag'])){
            if(is_array($data['Content']['flag'])){
                $data['Content']['flag'] = implode(',', $data['Content']['flag']);
            }else{
                $data['Content']['flag'] = $data['Content']['flag'];
            }
        }else{
            $data['Content']['flag'] = '';
        }
        if(isset($data['Content']['preview'])){
            if(is_array($data['Content']['preview'])){
                $data['Content']['preview'] = Tool::changeUploadFormat($data['Content']['preview']);
            }else if(is_null($data['Content']['preview'])){
                $data['Content']['preview'] = '';
            }
        }
        if(isset($data['Content']['outside'])){
            $data['Content']['outside'] = Tool::changeBool($data['Content']['outside']);
        }
        if(isset($data['Content']['member_view'])){
            $data['Content']['member_view'] = Tool::changeBool($data['Content']['member_view']);
        }
        if(isset($data['Content']['state']) and $data['Content']['state'] == 3){//说明是需要审核
            $data['Content']['audit_time'] = date('Y-m-d H:i:s', time());
        }
        if(isset($data['Content']['display'])){
            $data['Content']['display'] = Tool::changeBool($data['Content']['display']);
        }
        if(isset($data['Content']['istop'])){
            $data['Content']['istop'] = Tool::changeBool($data['Content']['istop']);
            if($data['Content']['istop'] == 2){
                $data['Content']['toptime'] = time();
            }else{
                $data['Content']['toptime'] = 0;
            }
        }else{
            $data['Content']['toptime'] = 0;
        }
        if(isset($data['Content']['content'])){
            $data['Content']['content'] = Html::encode($data['Content']['content']);
        }

        $data['Content']['lastmodify_time'] = date('Y-m-d H:i:s', time());
        // P($data);
        if(!is_null($content)){
            if($this->load($data) and $this->validate()){
                // P($data);
                if(isset($this->catid)){
                    $content->catid = $this->catid;
                }
                if(isset($this->catids)){
                    $content->catids = $this->catids;
                }
                if(isset($this->title)){
                    $content->title = $this->title;
                }
                if(isset($this->titlecolor)){
                    $content->titlecolor = $this->titlecolor;
                }
                if(isset($this->flag)){
                    $content->flag = $this->flag;
                }
                if(isset($this->preview)){
                    $content->preview = $this->preview;
                }
                if(isset($this->source)){
                    $content->source = $this->source;
                }
                if(isset($this->author)){
                    $content->author = $this->author;
                }
                if(isset($this->outside)){
                    $content->outside = $this->outside;
                }
                if(isset($this->url_target)){
                    $content->url_target = $this->url_target;
                }
                if(isset($this->outside_url)){
                    $content->outside_url = $this->outside_url;
                }
                if(isset($this->keyword)){
                    $content->keyword = $this->keyword;
                }
                if(isset($this->describe)){
                    $content->describe = $this->describe;
                }
                if(isset($this->content)){
                    $content->content = $this->content;
                }
                if(isset($this->state)){
                    $content->state = $this->state;
                }
                if(isset($this->rejected_reason)){
                    $content->rejected_reason = $this->rejected_reason;
                }
                if(isset($this->audit_time)){
                    $content->audit_time = $this->audit_time;
                }
                if(isset($this->member_view)){
                    $content->member_view = $this->member_view;
                }
                if(isset($this->previewtime)){
                    $content->previewtime = $this->previewtime;
                }
                if(isset($this->integral)){
                    $content->integral = $this->integral;
                }
                if(isset($this->display)){
                    $content->display = $this->display;
                }
                if(isset($this->istop)){
                    $content->istop = $this->istop;
                }
                if(isset($this->add_time)){
                    $content->add_time = $this->add_time;
                }
                $content->toptime = $this->toptime;
                if(isset($this->lastmodify_time)){
                    $content->lastmodify_time = $this->lastmodify_time;
                }
                // P($content);
                if($content->save(false)){
                    return true;
                }
            }
        };
        return false;
    }

    /*多个分类  把数组变成字符串*/
    private function changeCatidFormat($arr)
    {
        $res = [];
        if(is_array($arr)){
            for($i=0; $i<count($arr); $i++){
                $res[$i] = isset($arr[$i]['catid'])?$arr[$i]['catid']:"";
            }
        }
        if(!empty($res)){
            return implode(',', $res);
        }
        return $res;
    }


    /*获取内容的分类信息*/
    public function getCategory()
    {
        $info = $this->hasOne(ContentCategory::className(), ['catid' => 'catid'])
                    ->select(['{{%content_category}}.catid', 'alias']);
        return $info;
    }

    
}